﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using Benefit.Models.Sys;
namespace Benefit.Models.Month
{
    /// <summary>
    /// 小组月均值
    /// </summary>
    public class GroupAvgMonth
    {
        [Key]
        public int Id { get; set; }

        public int GroupId { get; set; }

        public virtual Groups Group { get; set; }

        /// <summary>
        /// 交易年
        /// </summary>
        public int Year { get; set; }
        /// <summary>
        /// 交易月
        /// </summary>
        public int Month { get; set; }
        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double Profit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Free { get; set; }
        /// <summary>
        /// 净收益
        /// </summary>
        public double DayCount { get; set; }
        /// <summary>
        /// 交易笔数
        /// </summary>
        public double BillCount { get; set; }
        /// <summary>
        /// 总亏损
        /// </summary>
        public double SumLost { get; set; }
        /// <summary>
        /// 总盈利
        /// </summary>
        public double SumWin { get; set; }
        /// <summary>
        /// 保证金占用
        /// </summary>
        public double UsedMargin { get; set; }

        /// <summary>
        /// 资金利用率
        /// </summary>
        public double Utilization { get; set; }
        /// <summary>
        /// 胜率
        /// </summary>
        public double WinRate { get; set; }
        /// <summary>
        /// 涨幅
        /// </summary>
        public double RaiseRate { get; set; }

        /// <summary>
        /// 每笔盈利
        /// </summary>
        public double AvgBillBenefit { get; set; }

        /// <summary>
        /// 超过止损次数
        /// </summary>
        public double OverStopLoss { get; set; }

        /// <summary>
        /// 风险次数
        /// </summary>
        public int RiskCount { get; set; }

        /// <summary>
        /// 收益率
        /// </summary>
        public double Yield { get; set; }
        /// <summary>
        /// 最大回撤
        /// </summary>
        public double MaxReturn { get; set; }
        /// <summary>
        /// 平均持仓时间
        /// </summary>
        public double AvgPostionTime { get; set; }
        /// <summary>
        /// 平进平出单量
        /// </summary>
        public int PingJinPing { get; set; }
        /// <summary>
        /// 盈利次数
        /// </summary>
        public int WinCount { get; set; }
        /// <summary>
        /// 亏损次数
        /// </summary>
        public int LoseCount { get; set; }



        ///// <summary>
        ///// 删除当月所有记录
        ///// </summary>
        ///// <param name="weekId"></param>
        //public void Delete(int month, int year, DB.DBManager db)
        //{
        //    var query = from t in db.GroupAvgMonth where t.Year == year && t.Month == month select t;
        //    foreach (Data.Month.GroupAvgMonth c in query)
        //    {
        //        db.GroupAvgMonth.Remove(c);
        //    }
        //    db.SaveChanges();
        //}


        //public GroupAvgMonth GetGroupAvgMonth(int groupId, int month, int year, DB.DBManager db)
        //{
        //    var query = db.GroupAvgMonth.Where(a => a.GroupId == groupId).Where(a => a.Month == month).Where(a => a.Year == year);
        //    if (query.Count() > 0)
        //    {
        //        return query.First();
        //    }
        //    else
        //    {
        //        return null;
        //    }
        //}

        ///// <summary>
        ///// 保存月小组均值
        ///// </summary>
        ///// <param name="tradeHistoryId">插入时间编号</param>
        ///// <param name="pdate">插入时间</param>
        //public void InitGroupMonthAvg(int month, int year, DB.DBManager db)
        //{
        //    List<Sys.Groups> groups = db.Groups.ToList();
        //    foreach (Sys.Groups g in groups)
        //    {
        //        GroupAvgMonth day = new GroupAvgMonth();
        //        day.GroupId = g.Id;
        //        day.Month = month;
        //        day.Year = year;

        //        var queryExist = from t in db.GroupAvgMonth where t.Month == month && t.Year == year && t.GroupId == g.Id select t;
        //        if (queryExist.Count() > 0)//有过当月的记录
        //        {
        //            day = queryExist.First();//找到这条记录
        //        }

        //        //dfdsfdsafdsafdsdsffsdfds这一段不对
        //        var query = from t in db.GroupAvgDay where t.TradeHistory.MPdate == month && t.TradeHistory.YPdate == year && t.GroupId == g.Id select t;
        //        if (query.Count() > 0)
        //        {
        //            day.BillCount = Math.Round(query.Average(a => a.BillCount), 2);
        //            day.DayCount = Math.Round(query.Average(a => a.DayCount), 2);
        //            day.Free = Math.Round(query.Average(a => a.Free), 2);
        //            day.LoseCount = Convert.ToInt32(query.Average(a => a.LoseCount));
        //            day.Profit = Math.Round(query.Average(a => a.Profit), 2);
        //            day.SumLost = Math.Round(query.Average(a => a.SumLost), 2);
        //            day.SumWin = Math.Round(query.Average(a => a.SumWin), 2);
        //            day.UsedMargin = Math.Round(query.Average(a => a.UsedMargin), 2);
        //            day.WinCount = Convert.ToInt32(query.Average(a => a.WinCount));
        //            day.AvgBillBenefit = Math.Round(query.Average(a => a.AvgBillBenefit), 2);
        //            day.AvgPostionTime = Math.Round(query.Average(a => a.AvgPostionTime), 2);
        //            day.MaxReturn = Math.Round(query.Average(a => a.MaxReturn), 2);
        //            day.OverStopLoss = Math.Round(query.Average(a => a.OverStopLoss), 2);
        //            day.PingJinPing = Convert.ToInt32(query.Average(a => a.PingJinPing));
        //            day.RaiseRate = Math.Round(query.Average(a => a.RaiseRate), 2);
        //            day.RiskCount = Convert.ToInt32(query.Average(a => a.RiskCount));
        //            day.Utilization = Math.Round(query.Average(a => a.Utilization), 2);
        //            day.WinRate = Math.Round(query.Average(a => a.WinRate), 2);
        //            day.Yield = Math.Round(query.Average(a => a.Yield), 2);

        //        }
        //        if (queryExist.Count() == 0)
        //        {
        //            db.GroupAvgMonth.Add(day);
        //        }
        //    }
        //    db.SaveChanges();
        //}
    }
}